package com.hmdp.config;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class LongToStringRedisSerializer implements RedisSerializer<Object> {

    @Override
    public byte[] serialize(Object value) throws SerializationException {
        if (value == null) {
            return new byte[0];
        }
        return String.valueOf(value).getBytes();
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        String value = new String(bytes);
        if (value.matches("\\d+")) { // 检查是否为数字
            return Long.parseLong(value);
        } else {
            return value;
        }
    }
}
